package com.shopping_center.management.service.impl;

import ch.qos.logback.classic.spi.EventArgUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shopping_center.management.common.DataWrapper;
import com.shopping_center.management.common.ResultCode;
import com.shopping_center.management.mapper.CarMapper;
import com.shopping_center.management.model.Car;
import com.shopping_center.management.model.DropDownList;
import com.shopping_center.management.model.OptionBean;
import com.shopping_center.management.service.CarService;
import com.shopping_center.management.utils.GenerateUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author YeFeng
 * @date 2023/2/2
 */
@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarMapper carMapper;

    @Override
    public void insertInToCarInfo(Car car) {
        Car carInfo = new Car();
        carInfo.setCarId(GenerateUUID.generateUUID());
        carInfo.setCarManufacturer(car.getCarManufacturer());
        carInfo.setCarGuidePrice(car.getCarGuidePrice());
        carInfo.setCarNumber(car.getCarNumber());
        carInfo.setCarEnergyType(car.getCarEnergyType());
        carInfo.setCarTimeToMarket(car.getCarTimeToMarket());
        carInfo.setCarMaximumPower(car.getCarMaximumPower());
        carInfo.setCarBodyStructure(car.getCarBodyStructure());
        carInfo.setCarAppearanceColor(car.getCarAppearanceColor());
        carInfo.setCarShiftingMode(car.getCarShiftingMode());
        carInfo.setCarDriveMode(car.getCarDriveMode());
        carInfo.setCarTransmissionType(car.getCarTransmissionType());
        carInfo.setCreatedBy("System");
        carInfo.setCreatedTime(new Timestamp(System.currentTimeMillis()));
        carInfo.setUpdatedBy("System");
        carInfo.setUpdatedTime(new Timestamp(System.currentTimeMillis()));
        carInfo.setVersion(1);
        carMapper.insertInToCarInfo(carInfo);
    }

    @Override
    public PageInfo<Car> selectAllCarInfo(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Car> cars = carMapper.selectAllCarInfo();
        if (!CollectionUtils.isEmpty(cars)) {
            PageInfo<Car> pageInfo = new PageInfo<>(cars);
            return pageInfo;
        }
        return null;
    }

    @Override
    public DropDownList selectAllCarInfo() {
        DropDownList dropDownList = new DropDownList();

        List<Car> cars = carMapper.selectAllCarInfo();

        List<OptionBean> carManufacturerList = new ArrayList<>();
        for (String carManufacturer : cars.stream().map(Car::getCarManufacturer).distinct().collect(Collectors.toList())) {
            OptionBean optionBean = new OptionBean();
            optionBean.setText(carManufacturer);
            optionBean.setValue(carManufacturer);
            carManufacturerList.add(optionBean);
        }
        carManufacturerList.sort(Comparator.comparing(OptionBean::getValue));

        List<OptionBean> carEnergyTypeList = new ArrayList<>();
        for (String carEnergyType : cars.stream().map(Car::getCarEnergyType).distinct().collect(Collectors.toList())) {
            OptionBean optionBean = new OptionBean();
            optionBean.setText(carEnergyType);
            optionBean.setValue(carEnergyType);
            carEnergyTypeList.add(optionBean);
        }
 
        List<OptionBean> carAppearanceColorList = new ArrayList<>();
        for (String carAppearanceColor : cars.stream().map(Car::getCarAppearanceColor).distinct().collect(Collectors.toList())) {
            OptionBean optionBean = new OptionBean();
            optionBean.setText(carAppearanceColor);
            optionBean.setValue(carAppearanceColor);
            carAppearanceColorList.add(optionBean);
        }

        dropDownList.setCarManufacturerList(carManufacturerList);
        dropDownList.setCarEnergyTypeList(carEnergyTypeList);
        dropDownList.setAppearanceColorList(carAppearanceColorList);

        return dropDownList;
    }

    @Override
    public void editCarInfo(Car car) {
        Car carInfo = new Car();
        carInfo.setCarId(car.getCarId());
        carInfo.setCarManufacturer(car.getCarManufacturer());
        carInfo.setCarGuidePrice(car.getCarGuidePrice());
        carInfo.setCarNumber(car.getCarNumber());
        carInfo.setCarEnergyType(car.getCarEnergyType());
        carInfo.setCarTimeToMarket(car.getCarTimeToMarket());
        carInfo.setCarMaximumPower(car.getCarMaximumPower());
        carInfo.setCarBodyStructure(car.getCarBodyStructure());
        carInfo.setCarAppearanceColor(car.getCarAppearanceColor());
        carInfo.setCarShiftingMode(car.getCarShiftingMode());
        carInfo.setCarDriveMode(car.getCarDriveMode());
        carInfo.setCarTransmissionType(car.getCarTransmissionType());
        carInfo.setUpdatedBy("System");
        carInfo.setUpdatedTime(new Timestamp(System.currentTimeMillis()));
        carInfo.setVersion((car.getVersion() + 1));

        carMapper.editCarInfo(carInfo);
    }

    @Override
    public void deleteCarInfoById(String carId) {
        carMapper.deleteCarInfoById(carId);
    }

    @Override
    public List<Car> selectAllCarInfoIntoExecl() {
        return carMapper.selectAllCarInfoIntoExecl();
    }

    @Override
    public PageInfo<Car> selectCarInfoByFilter(int pageNum, int pageSize, String carManufacturer, String carEnergyType, String carAppearanceColor) {
        PageHelper.startPage(pageNum, pageSize);
        List<Car> cars = carMapper.selectCarInfoByFilter(carManufacturer, carEnergyType, carAppearanceColor);
        if (!CollectionUtils.isEmpty(cars)) {
            PageInfo<Car> pageInfo = new PageInfo<>(cars);
            return pageInfo;
        }
        return null;
    }
}
